package com.qingkai.takeawayorders.repository

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.qingkai.takeawayorders.category.CategoryModel
import com.qingkai.takeawayorders.category.FoodModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext
import org.json.JSONObject

class MainRepository {


    suspend fun loadCategoryList(): LiveData<MutableList<CategoryModel>> =
        withContext(Dispatchers.IO) {
            var listData = MutableLiveData<MutableList<CategoryModel>>()
            val category = JSONObject(json)
            val categoryList = category.getJSONArray("Category")
            val lists = mutableListOf<CategoryModel>()
            for (i in 0 until categoryList.length()) {
                val category = categoryList.getJSONObject(i)
                val categoryModel = CategoryModel()
                categoryModel.id = category.getInt("id")
                categoryModel.title = category.getString("title")
                categoryModel.picUrl = category.getString("picUrl")
//                "https://img2.baidu.com/it/u=2600488553,1309696642&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=280"
                lists.add(categoryModel)
            }
            listData.postValue(lists)
            delay(3000)
            return@withContext listData
        }

    fun loadPopular(): LiveData<MutableList<FoodModel>> {
        var listData = MutableLiveData<MutableList<FoodModel>>()
        val category = JSONObject(json)
        val categoryList = category.getJSONArray("Items")
        val lists = mutableListOf<FoodModel>()
        for (i in 0 until categoryList.length()) {
            val category = categoryList.getJSONObject(i)
            val categoryModel = FoodModel()
            categoryModel.description = category.getString("description")
            categoryModel.categoryId = category.getString("categoryId")
            categoryModel.price = category.getDouble("price")
            categoryModel.title = category.getString("title")
            categoryModel.picUrl =
                "https://img2.baidu.com/it/u=2600488553,1309696642&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=280"
            lists.add(categoryModel)
        }
        listData.value = lists
        return listData
    }


    val json = "{\"Category\":[\n" +
            "{\n" +
            "\t\"id\": 8,\n" +
            "\t\"picUrl\": \"https://b0.bdstatic.com/ugc/BVfjhxryVNA5tsJMO8MneQ42cd37900ba55e9d4d6679f53dd1dc6e.jpg\",\n" +
            "\t\"title\": \"pizza\"\n" +
            "},\n" +
            "{\"id\": 1,\n" +
            "\"picUrl\":\"https://img2.baidu.com/it/u=2600488553,1309696642&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=280\",\n" +
            "\"title\": \"Burger\"},\n" +
            "{\"id\":2,\n" +
            "\"picUrl\":\"https://img2.baidu.com/it/u=2600488553,1309696642&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=280\",\n" +
            "\"title\" : \"Hotdog\"},\n" +
            "{\"id\": 3,\n" +
            "\"picUrl\":\"https://img2.baidu.com/it/u=2600488553,1309696642&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=280\",\n" +
            "\"title\":\"Drink\"},\n" +
            "{\"id\":4,\n" +
            "\"picUrl\":\"https://img2.baidu.com/it/u=2600488553,1309696642&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=280\",\n" +
            "\"title\" :\"Donut\"\n" +
            "}\n" +
            "],\n" +
            "\"Items\" : [\n" +
            "{\"categoryId\":\"2\",\n" +
            "\"description\": \"Slices of pepperon!, mozzarella cheese, fresh oregano, ground black pepper, pizza sauce\",\n" +
            "\"picUrl\": \"https://img2.baidu.com/it/u=2600488553,1309696642&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=280\",\n" +
            "\"price\":9.76,\n" +
            "\"title\": \"pepperoni pizza\"},\n" +
            "{\"categoryId\":\"0\",\n" +
            "\"description\": \"lorem iosm dolor sit anet, consectetur adipiscin elt, Sed do elsod temno incidldunt ut iahore et daiore nams slioua. it enim ad mininenien, auls nostrud exercitation pllamc lsbris nisi ut aliquip exea comdo cansequat. buls aute irure dolor in reprehenderit in voluptate velit ese cillum doloriu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proldent, sunt in culpa qul offica deserunt .Excepteur sint ocaecat\",\n" +
            "\"picUrl\": \"https://img2.baidu.com/it/u=2600488553,1309696642&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=280\",\n" +
            "\"price\": 32,\n" +
            "\"title\":\"Vegetable pizza\"\n" +
            "}\n" +
            "]\n" +
            "}"
}